﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class principal : Form
    {
        public principal()
        {
            InitializeComponent();  
      
           
        }

        private void initcouleur()
        {

            DataRowCollection rows = donnees.Personne.Rows;
            WindowsFormsApplication1.Donnees.ParametreDataTable tableparametre = donnees.Parametre;
            foreach (DataRow rowd in rows)
            {

                DataRow[] rowparam = tableparametre.getCouleurByUserId(rowd[WindowsFormsApplication1.Donnees.PersonneDataTable.FIELD_ID].ToString());
                if (rowparam.Length != 0)
                {
                    DataRow rowparamfirst = rowparam.First();
                    DataGridViewRow rowFound = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => test(r, (int)rowd[WindowsFormsApplication1.Donnees.PersonneDataTable.FIELD_ID])).SingleOrDefault();
                    if (rowFound != null)
                    {
                        ColorConverter converteur = new ColorConverter();
                        DataGridViewCell rowcolor = rowFound.Cells[WindowsFormsApplication1.Donnees.PersonneDataTable.COLUMN_FIELD_BUTTONCOULEUR];
                        rowcolor.Style.BackColor = (Color)converteur.ConvertFromString(rowparamfirst[WindowsFormsApplication1.Donnees.ParametreDataTable.FIELD_VALEUR].ToString());
                    }
                }
            }
        }


        private void initTableau()
        {
           try{
               donnees.ReadXml(Properties.datausers.Default.bddpath + Properties.datausers.Default.fichiername);
           }
            catch (Exception )
           {
               ChangeBddsettings();
            }
          
           
        }



        private void ChangeBddsettings()
         {
             folderBrowserDialog1.Description = "Veuillez choisir un dossier pour lA sauvegarde de vos données";
             folderBrowserDialog1.ShowDialog();
             Properties.datausers.Default.Reload();
             Properties.datausers.Default.bddpath = folderBrowserDialog1.SelectedPath + "\\";
             Properties.datausers.Default.Save();

             save();
            initTableau();
        
        
        }

        private void initForm(object sender, EventArgs e)
        {
            initTableau();
            initcouleur();
        }




        private void button1_Click(object sender, EventArgs e)
        {           
          vacancesconf formdate = new vacancesconf();
          formdate.ShowDialog();
        }


        private Boolean test(DataGridViewRow r,int valeur)
        {

            Boolean ok = false;
            if (r != null)
            {
                if (r.Cells[0].Value != null)
                {
                    ok = (int)r.Cells[0].Value == valeur;
                }            
            }
            return ok;

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView view = (DataGridView)sender;
            this.InvokeLostFocus(view, e);
            save();
            colorDialog1.ShowDialog();            

            int userid = (int)view[WindowsFormsApplication1.Donnees.PersonneDataTable.FIELD_ID, e.RowIndex].Value;

            WindowsFormsApplication1.Donnees.ParametreDataTable tableparametre = donnees.Parametre;

            DataRow[] row = tableparametre.getCouleurByUserId(userid.ToString());

            if (e.RowIndex >= -1)
            {
                DataRow rowuser = null;
                if (row.Length == 0)
                    rowuser = tableparametre.AddCouleurByUserId(userid.ToString());
                else
                    rowuser = row[0];

                DataGridViewButtonCell buttoncell = (DataGridViewButtonCell)view[e.ColumnIndex, e.RowIndex];
                buttoncell.Style.BackColor = colorDialog1.Color;
                buttoncell.Selected = false;
                ColorConverter converteur = new ColorConverter();
                rowuser[WindowsFormsApplication1.Donnees.ParametreDataTable.FIELD_VALEUR] = converteur.ConvertToString(colorDialog1.Color);

            }       
        }

        private void enregistrerToolStripButton_Click_1(object sender, EventArgs e) {

            save();
        }



        public void save()
        {
           
            donnees.WriteXml(Properties.datausers.Default.bddpath + Properties.datausers.Default.fichiername);
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

    
    }
}